
var pt = document.getElementById("paintP");
var ptctx = pt.getContext("2d");

var fighter=new Fighter();
var mousepoint=new Point();

var adio=new Audio();
adio.src="sound/shoot1.wav";
let weaponimage = new Image();
var stageimage=new Image();
var djimage=new Image();
djimage.src="images/dj.png";
//adio.loop=true;

//
var bteffects=new Array();

var enemys=new Array();

//自动装弹
function autoreloadammou()
{
    //自动装弹
    if(fighter.ammunitionbak>0) {
        adio.src = "sound/change.wav";
        adio.play();
        fighter.reloadammou = true,
            // fighter.ammunition = ammous,
        fighter.actiontype = "换弹夹";
        fighter.actionindex = 0;
        fighter.actionstart_y = 515;
        fighter.action_width = 265;
        fighter.actioncount = 2;
        fighter.action_height = 265;
        fighter.ratewidth = 60;
        fighter.rateheight = 80;
        fighter.delay = 1000;

        if(fighter.ammunitionbak - (fighter.ammunitionlimit-fighter.ammunition)>=0) {
            fighter.ammunitionbak = fighter.ammunitionbak - (fighter.ammunitionlimit - fighter.ammunition);
            fighter.ammunition = fighter.ammunitionlimit;
        }
        else {
            fighter.ammunition =fighter.ammunition +fighter.ammunitionbak;
            fighter.ammunitionbak = 0;
        }
       //fighter.reloadammou=false;
       //
    }
}

pt.oncontextmenu=function(e){
    return false;
}

//鼠标操作按下
pt.onmousedown = function (e) {
    if (e.button.toString() === "0") {
        if(fighter.ammunition>0) {
            //左键操作
            fighter.isfire = true;
            adio.src = "sound/shoot1.wav";
        }

    } else if (e.button.toString() === "2") {
        //右键操作
        autoreloadammou();
    }
}
//鼠标抬起
pt.onmouseup = function (e) {
    fighter.isfire=false;
    //adio.pause();
}

pt.onmousemove = function (e) {
    let pp = getXY(pt, e);
    if (pp.x < (pt.width - 50))
    {
        mousepoint=pp;
    }

}

this.onload = function () {

    pt.width = 600;
    pt.height = 500;
    let p1 = new Point();

    //敌人
    for(let i=0;i<100;i++)
    {
        var eny=new Enemy();
        eny.en_x=Math.floor(Math.random() * (600 - 0 + 1)) + 0;
        eny.en_y= Math.floor(Math.random() * (280 - 100 + 1)) + 100;
        eny.speed=Math.floor(Math.random() * (20 - 5 + 1)) + 5;
        enemys.push(eny);
    }

    for(let i=0;i<400;i++)
    {
       var beffct=new Bulleteffect();
       beffct.img.src="images/zdtx.png";
        bteffects.push(beffct);
    }

   // console.log(bteffects);
    weaponimage.src = "images/weapons.png";
    stageimage.src="images/stage5.png";
    fighter=new Fighter("action3",
        0,360,280,135,
        3,0,"hhh",100,100,
        5,false,32,32);


    let waringflag=false;

    var timer = setInterval(function () {
        pt.style.cursor = "url(images/cus1.png) 20 20,auto"
        if (ptctx != null) {

            // if(!fighter.reloadammou) {
            //     adio.src = "sound/shoot1.wav";
            // }
            if (!fighter.reloadammou && fighter.isfire &&  fighter.ammunition> 0 ) {
                fighter.ammunition = fighter.ammunition - 1;

                for(let i=0;i<bteffects.length;i++)
                {
                    if(!bteffects[i].visable)
                    {
                       // console.log(bteffects[i])
                        bteffects[i].visable=true;
                        bteffects[i].x=mousepoint.x;
                        bteffects[i].y=mousepoint.y;
                        break;
                    }
                }
                adio.play();
                if (fighter.actionindex < fighter.actioncount-1)
                    fighter.actionindex = fighter.actionindex + 1;
                else
                    fighter.actionindex = 0;

                if(fighter.ammunition<=0) {
                    autoreloadammou(); //自动装弹
                }
            }
            else {
                if(!fighter.reloadammou) {
                    fighter.actionindex = 0;
                }
                else
                {
                    if (fighter.actionindex < fighter.actioncount) {

                        var timerintelver=setTimeout(()=>{
                            fighter.actionindex = fighter.actionindex + 1;
                        },200)
                    }
                    else {
                        fighter.reloadammou = false;
                        fighter.actionindex = 0;
                        fighter.actiontype = "射击";
                        fighter.reloadammou = false;
                        fighter.actionstart_y = 360;
                        fighter.action_width = 280;
                        fighter.actioncount = 3;
                        fighter.action_height = 135;
                        fighter.ratewidth = 50;
                        fighter.rateheight = 50;
                        fighter.delay = 100;
                        adio.src = "sound/shoot1.wav";
                    }
                }
            }
            ptctx.beginPath();
            ptctx.clearRect(0, 0, pt.width, pt.height);
            ptctx.rect(0, 0, pt.width - 1, pt.height - 1);
            ptctx.stroke();
            ptctx.rect(0, 400, pt.width - 1, 100);
            ptctx.stroke();
          //  ptctx.fillStyle = 'rgba(0, 255, 255, 0)';
            //action1  265,180    2    x:0 y:0
            //action2
            //action3  280,135    3    x:0 y:360
            //绘制背景图
            ptctx?.drawImage(stageimage, 0 ,
                0, 600, 400);
            ptctx?.drawImage(weaponimage, 0 + fighter.action_width * fighter.actionindex,
                fighter.actionstart_y, fighter.action_width, fighter.action_height, mousepoint.x, 400-fighter.rateheight,
                fighter.ratewidth , fighter.rateheight);

            //信息描述
            ptctx.font = "oblique small-caps bold 16px Arial";
            ptctx.fillStyle = "blue";
            let astr=fighter.ammunition.toString();
            let ammunitionstr = "弹药：" + astr + "/" + fighter.ammunitionlimit.toString() ;  //fighter.ammuninfostr;
            ptctx.fillText(ammunitionstr, 5, 430);


            ptctx?.drawImage(djimage, 5 ,
                    440,20,40);
            ptctx.fillText("备弹:"+fighter.ammunitionbak, 25, 480);


            ptctx.font = "oblique small-caps bold 16px Arial";
            ptctx.fillStyle = "red";
            ptctx.fillText("杀敌："+fighter.kills.toString(), 100, 430);


           // ptctx?.drawImage(bteffects[0].img,0,0,127,127,10,10,20,20);
            for(let i=0;i<bteffects.length;i++)
            {
                if(bteffects[i].visable)
                {
                    ptctx?.drawImage(bteffects[i].img, bteffects[i].img_x ,
                        bteffects[i].img_y, bteffects[i].dwidth,bteffects[i].dheight,
                        bteffects[i].x-bteffects[i].rwidth,
                        bteffects[i].y-bteffects[i].rheight,bteffects[i].rwidth,bteffects[i].rheight);
                   // console.log(bteffects[i]);
                }
            }
            for(let i=0;i<enemys.length;i++)
            {
                if(enemys[i].enlive>0)
                {
                    if(fighter.isfire) {
                        if (mousepoint.x + 5 >= enemys[i].en_x && mousepoint.x + 5 <= enemys[i].en_x + enemys[i].enwidth) {
                            if (mousepoint.y + 5 > enemys[i].en_y && mousepoint.y + 5 <= enemys[i].en_y + enemys[i].enheight) {
                                enemys[i].enlive = enemys[i].enlive - fighter.demage;
                                if (enemys[i].enlive <= 0) {
                                    fighter.kills = fighter.kills + 1;
                                }
                            }
                        }
                    }
                }
            }

            //绘制死人
            for(let i=0;i<enemys.length;i++)
            {
                let eImg=new Image();
                let enpath="images";
               // eImg.src=enpath+"/l/diedl_1.png";
                if(enemys[i].enlive<=0)
                {
                    enemys[i].enwidth=40;
                    enemys[i].enheight=30;

                    //死亡
                    if(enemys[i].fun == "l")
                    {
                        enpath = "images/l";
                        if( eImg.src==enpath + "/diedl_0.png")
                            eImg.src=enpath + "/diedl_1.png";
                        else
                            eImg.src=enpath + "/diedl_0.png";
                    }
                    else
                    {
                        enpath = "images/r";
                        if( eImg.src==enpath + "/diedr_0.png")
                            eImg.src=enpath + "/diedr_1.png";
                        else
                            eImg.src=enpath + "/diedr_0.png";
                    }
                    ptctx?.drawImage(eImg, 0, 0,eImg.width,eImg.height,enemys[i].en_x,enemys[i].en_y+10,enemys[i].enwidth,enemys[i].enheight
                    );
                }

            }


            //绘制活人
            for(let i=0;i<enemys.length;i++)
            {
                let eImg=new Image();
                let enpath="images"
                if(enemys[i].enlive>0) {
                    if (enemys[i].fun == "l") {
                        if (enemys[i].en_x > 0)
                            enemys[i].en_x = enemys[i].en_x - enemys[i].speed;
                        else {
                            enemys[i].en_x = 0;
                            enemys[i].fun = "r";
                        }
                        enpath = "images/l";
                    } else {
                        if (enemys[i].en_x < pt.width - 50) {
                            enemys[i].en_x = enemys[i].en_x + enemys[i].speed;
                        } else {
                            enemys[i].en_x = pt.width - 50;
                            enemys[i].fun = "l";
                        }
                        enpath = "images/r";
                    }

                    if (enemys[i].enactionindex < enemys[i].enactioncount) {
                        eImg.src = enpath + "/" + enemys[i].enactionindex.toString() + ".png";
                        enemys[i].enactionindex = enemys[i].enactionindex + 1;
                    } else {
                        eImg.src = enpath + "/1.png";
                        enemys[i].enactionindex = 1;
                    }
                    ptctx?.drawImage(eImg, 0, 0,eImg.width,eImg.height,enemys[i].en_x,enemys[i].en_y,enemys[i].enwidth,enemys[i].enheight
                    );
                }


            }

            if(fighter.ammunitionbak<=0)
            {
                ptctx.font = "oblique small-caps bold 35px Arial";
                if(waringflag) {
                    ptctx.fillStyle = "yellow";
                    waringflag=false;
                }
                else {
                    ptctx.fillStyle = "red";
                    waringflag=true;
                }
                ptctx.fillText("备用弹药已经耗尽！！！！", pt.width/2-150, pt.height/2);
                ptctx.fillText("补给等待"+fighter.ammundelayindex+"秒", pt.width/2-80, pt.height/2+38);
                ptctx.stroke();
            }

        }
    },100);
    var timer = setInterval(()=>{

        if(fighter.ammunitionbak==0)
        {
            if(fighter.ammundelayindex >0)
               fighter.ammundelayindex=fighter.ammundelayindex-1;
            else
            {
                fighter.ammunitionbak=300;
                fighter.ammundelayindex=20;
            }
        }

        for(let i=0;i<bteffects.length;i++)
        {
            if(bteffects[i].visable)
            {

                if(bteffects[i].delayindex>=bteffects[i].delay)
                {
                    bteffects[i].delayindex=0;
                    bteffects[i].visable=false;
                }
                else {
                    bteffects[i].delayindex = bteffects[i].delayindex + 1;
                }

            }
        }

    },1000)
}


